package com.crm.biz.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.crm.model.BizContract;
import com.crm.model.BizPartner;
import com.crm.service.BizContractService;
import com.crm.service.BizPartnerService;
import com.crm.util.FdfsUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.UUIDUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizContract")
public class BizContractController {
	@Autowired
	private BizContractService bizContractService;
	@Autowired
	private BizPartnerService bizPartnerService;
	
	@RequestMapping("/index")
	public String index(ModelMap map,String partnerId){
		BizPartner partner = bizPartnerService.selectByPartnerId(partnerId);
		map.put("partner", partner);
		return "configuration/contractManage";
	}
	
	@RequestMapping("/selectByPartnerId")
	@ResponseBody
	private BizPartner selectByPartnerId(String partnerId) {
		BizPartner partner = bizPartnerService.selectByPartnerId(partnerId);
		return partner;
	}

	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getAll(Page<BizContract> page, BizContract t){
		Map<String, Object> map = new HashMap<>();
		Page<BizContract> list = (Page<BizContract>) bizContractService.selectAll(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "查询成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(BizContract record){
		Map<String, Object> map = new HashMap<>();
		
		String uuid = UUIDUtil.getUUID();
		record.setContractId(uuid);
		bizContractService.insert(record);
		
		map.put("contractId", uuid);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizContract record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizContractService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	@RequestMapping("/load")
	@ResponseBody
	public void load(HttpServletResponse response,HttpServletRequest request, Page<BizContract> page, BizContract t){
		Page<BizContract> list = (Page<BizContract>) bizContractService.selectAll(page, t);
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/constractUpload")
	@ResponseBody
	public Map<String, Object> constractUpload(HttpServletRequest request,
		@RequestParam("file_data") MultipartFile uploadFile) throws Exception{
		Map<String,Object> retMap = new HashMap<>();
		String originalFileName = uploadFile.getOriginalFilename();
		String fileExt = originalFileName.substring(originalFileName.lastIndexOf(".") + 1).toLowerCase();
		BufferedImage img = ImageIO.read(uploadFile.getInputStream());
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ImageIO.write(img, fileExt, bos);
		String files[] = {};
		files = FdfsUtil.uploadFile(bos.toByteArray(), originalFileName,fileExt,uploadFile.getSize());
		if(!ValidateUtil.isEmpty(files)){
			retMap.put("url",files[0] + "/" + files[1]);
		}
		return retMap;
	}
}
